<script setup>
import { get_Student } from '@/request';
import * as echarts from 'echarts';
import moment from 'moment';
import { onMounted, ref } from 'vue';
var option = {
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine'],
        textStyle: { color: '#fff' },
        icon: 'circle'
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
        axisLine: {
            lineStyle: {
                color: '#FFFFFF'
            }
        },
    },
    yAxis: {
        type: 'value',
        axisLine: {
            lineStyle: {
                color: '#FFFFFF'
            }
        },
    },
    series: [
        {
            name: 'Email',
            type: 'line',
            stack: 'Total',
            data: [120, 132, 101, 134, 90, 230, 210],
            smooth: true,
            showSymbol: false
        },
        {
            name: 'Union Ads',
            type: 'line',
            stack: 'Total',
            data: [220, 182, 191, 234, 290, 330, 310],
            smooth: true,
            showSymbol: false
        },
        {
            name: 'Video Ads',
            type: 'line',
            stack: 'Total',
            data: [150, 232, 201, 154, 190, 330, 410],
            smooth: true,
            showSymbol: false
        },
        {
            name: 'Direct',
            type: 'line',
            stack: 'Total',
            data: [320, 332, 301, 334, 390, 330, 320],
            smooth: true,
            showSymbol: false
        },
        {
            name: 'Search Engine',
            type: 'line',
            stack: 'Total',
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            smooth: true,
            showSymbol: false
        }
    ]
};

onMounted(() => {
    get_Student().then(resp => {
        console.log('学生发布情况', resp)
        var chartDom = document.getElementById('student');
        var myChart = echarts.init(chartDom);
        option.legend.data = resp.data.data.result[0].legend.data
        option.series = resp.data.data.result[0].series.map(item => {
            return {
                name: item.name,
                type: item.type,
                stack: item.stack,
                data: item.data,
                smooth: true,
                showSymbol: false
            }
        })
        const ws = new WebSocket('ws://localhost:3002/bigscreen/websockets?id==555')
        const onmessage = (e) => {
            const data = JSON.parse(e.data)
            option.legend.data = data.legendData
            option.series = data.seriesData.map((item, index) => {
                return {
                    name: data.legendData[index],
                    type: 'line',
                    stack: 'Total',
                    data: item,
                    //去掉小圆点
                    symbol: 'none',
                    //曲线
                    smooth: true
                }
            })
            sj()
            myChart.setOption(option)
        }
        ws.addEventListener('message', onmessage)
        //三秒
        setInterval(() => {
            // var myChart = echarts.init(chartDom)
            sj()
            // myChart.setOption(option)
        }, 3000)
        //适配

        const sj = () => {
            let XData = []
            let time = +new Date()
            for (let i = 0; i < 7; i++) {
                XData.unshift(moment(time).format('HH:mm:ss'))
                time -= 2 * 60 * 1000
            }
            option.xAxis.data = XData
        }

        option && myChart.setOption(option);
        window.addEventListener('resize', function () {
            myChart.resize();
        });
    })
})

</script>

<template>
    <div id="student" ref="Student">

    </div>
</template>

<style scoped lang="scss">
#student {
    width: 100%;
    height: 100%;
}
</style>
